all files / timePicker/ clockHours.vue

14.75% Statements 9/61
16.67% Branches 4/24
8.33% Functions 1/12
7.27% Lines 4/55
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                  1× 1× 1×                                                                                                                                                                                                                                                  
//
//
//
//
//
//
//
//
 
import clockNumber from './clockNumber'
import clockPointer from './clockPointer'
import {getTouchEventOffsetValues, rad2deg} from './timeUtils'
export default {
  props: {
    format: {
      type: String,
      default: 'ampm',
      validator (val) {
        return ['ampm', '24hr'].indexOf(val) !== -1
      }
    },
    initialHours: {
      type: Number,
      default: new Date().getHours()
    }
  },
  computed: {
    hours () {
      const hourSize = this.format === 'ampm' ? 12 : 24
      const hours = []
      for (let i = 1; i <= hourSize; i++) {
        hours.push(i % 24)
      }
      return hours
    }
  },
  methods: {
    getSelected () {
      let hour = this.initialHours
      if (this.format === 'ampm') {
        hour %= 12
        hour = hour || 12
      }
      return hour
    },
    isMousePressed (event) {
      if (typeof event.buttons === 'undefined') {
        return event.nativeEvent.which
      }
      return event.buttons
    },
    handleUp (event) {
      event.preventDefault()
      this.setClock(event, true)
    },
    handleMove (event) {
      event.preventDefault()
      if (this.isMousePressed(event) !== 1) return
      this.setClock(event, false)
    },
 
    handleTouchMove (event) {
      event.preventDefault()
      this.setClock(event.changedTouches[0], false)
    },
 
    handleTouchEnd (event) {
      event.preventDefault()
      this.setClock(event.changedTouches[0], true)
    },
 
    setClock (event, finish) {
      if (typeof event.offsetX === 'undefined') {
        const offset = getTouchEventOffsetValues(event)
 
        event.offsetX = offset.offsetX
        event.offsetY = offset.offsetY
      }
      const hours = this.getHours(event.offsetX, event.offsetY)
      this.$emit('change', hours, finish)
    },
 
    getHours (offsetX, offsetY) {
      const step = 30
      const x = offsetX - this.center.x
      const y = offsetY - this.center.y
      const cx = this.basePoint.x - this.center.x
      const cy = this.basePoint.y - this.center.y
 
      const atan = Math.atan2(cx, cy) - Math.atan2(x, y)
 
      let deg = rad2deg(atan)
      deg = Math.round(deg / step) * step
      deg %= 360
 
      let value = Math.floor(deg / step) || 0
 
      const delta = Math.pow(x, 2) + Math.pow(y, 2)
      const distance = Math.sqrt(delta)
 
      value = value || 12
      if (this.format === '24hr') {
        if (distance < 90) {
          value += 12
          value %= 24
        }
      } else {
        value %= 12
      }
 
      return value
    }
 
  },
  mounted () {
    const clockElement = this.$refs.mask
    this.center = {
      x: clockElement.offsetWidth / 2,
      y: clockElement.offsetHeight / 2
    }
 
    this.basePoint = {
      x: this.center.x,
      y: 0
    }
  },
  components: {
    'clock-number': clockNumber,
    'clock-pointer': clockPointer
  }
}